clear all
set printcolor gs1, permanently
set more off
cap log close
program drop _all
set matsize 11000, permanently

use "INTERMEDIATE/product_RMS_def10_firm_sampleE_06_15.dta", clear

*   New Variables
**************************
* Outcome variables
gen logprice = log(price)
gen logquantity = log(quantity)
gen logrevenue  = log(revenue)

gen logquantity_store     = log(quantity/num_store)
gen logquantity_storeweek = log(quantity/num)
gen logrevenue_store      = log(revenue/num_store)
gen logrevenue_storeweek  = log(revenue/num)

gen logstore=log(num_store)

* Fixed-effects
egen fe=group(module quarter)
qui tab cohort, gen(cohort_d)
qui tab quarter, gen(quarter_d)

keep if cohort>=3 & cohort<=23 // 19
drop if age>16 
drop if age==0
drop if maxAge<=16
keep if flag_longitudinal=="Complete"

foreach num of numlist 5/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d4-(`num'-2)*cohort_d3)
}

// Revenue
areg logrevenue i.age cohort_nd*, absorb(fe) cluster(module)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se
drop cohort_nd* 

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

twoway ///
(rarea coef_u coef_l age if age<=16, pstyle(ci)) ///
(connected coef age if age<=16, color(midblue) lwidth(thick)) ///
, ytitle("Estimated Sales (log scale)") xtitle("Age (quarters)") xlabel(4(4)16) ///
title("") ///
legend(off) ///
graphregion(color(white)) plotregion(fcolor(white)) note("")
graph export "OUTPUT/Figure2.eps", replace

